package com.ldj.struts.action;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.ldj.common.SystemSession;
import com.ldj.hibernate.ViewCaseDAO;

public class ViewCaseAction extends DispatchAction {

	public ActionForward find(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String year=new SimpleDateFormat("yyyy").format(Calendar.getInstance().getTime());
		int y = Integer.parseInt(year); 
		int endYear = y + 2;
		request.setAttribute("endYear", endYear);
		request.setAttribute("nowYear", y);
		return mapping.findForward("find");
	}
	
	public ActionForward detail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		ViewCaseDAO dao = new ViewCaseDAO(request, response);
		String areaCode = request.getParameter("areaCode");
		String year = request.getParameter("year");
		String type = request.getParameter("type");
		String startDate = year;
		String endDate = year;
		if(type.equals("0")){
			startDate += "-01-01";
			endDate += "-12-31";
		}else if(type.equals("1")){
			startDate += "-01-01";
			endDate += "-06-30";
		}else if(type.equals("2")){
			startDate += "-07-01";
			endDate += "-12-31";
		}else if(type.equals("3")){
			startDate += "-01-01";
			endDate += "-03-31";
		}else if(type.equals("4")){
			startDate += "-04-01";
			endDate += "-06-30";
		}else if(type.equals("5")){
			startDate += "-07-01";
			endDate += "-09-30";
		}else if(type.equals("6")){
			startDate += "-10-01";
			endDate += "-12-31";
		}
		List list = dao.findFromCode(areaCode, startDate, endDate);
		request.setAttribute("list", list);
		request.setAttribute("obj", dao.findTypeFromCode(areaCode, startDate, endDate));
		request.setAttribute("organ", SystemSession.getUserOrgan(request));
		return mapping.findForward("detail");
	}
	
	public ActionForward xls(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		ViewCaseDAO dao = new ViewCaseDAO(request, response);
		String areaCode = request.getParameter("areaCode");
		String year = request.getParameter("year");
		String type = request.getParameter("type");
		String startDate = year;
		String endDate = year;
		if(type.equals("0")){
			startDate += "-01-01";
			endDate += "-12-31";
		}else if(type.equals("1")){
			startDate += "-01-01";
			endDate += "-06-30";
		}else if(type.equals("2")){
			startDate += "-07-01";
			endDate += "-12-31";
		}else if(type.equals("3")){
			startDate += "-01-01";
			endDate += "-03-31";
		}else if(type.equals("4")){
			startDate += "-04-01";
			endDate += "-06-30";
		}else if(type.equals("5")){
			startDate += "-07-01";
			endDate += "-09-30";
		}else if(type.equals("6")){
			startDate += "-10-01";
			endDate += "-12-31";
		}
		List list = dao.findFromCode(areaCode, startDate, endDate);
		request.setAttribute("list", list);
		request.setAttribute("obj", dao.findTypeFromCode(areaCode, startDate, endDate));
		request.setAttribute("organ", SystemSession.getUserOrgan(request));
		return mapping.findForward("xls");
	}

	public ActionForward xgFind(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String year=new SimpleDateFormat("yyyy").format(Calendar.getInstance().getTime());
		int y = Integer.parseInt(year); 
		int endYear = y + 2;
		request.setAttribute("endYear", endYear);
		request.setAttribute("nowYear", y);
		return mapping.findForward("find");
	}
	
	public ActionForward xgDetail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		ViewCaseDAO dao = new ViewCaseDAO(request, response);
		String areaCode = request.getParameter("areaCode");
		String year = request.getParameter("year");
		String type = request.getParameter("type");
		String startDate = year;
		String endDate = year;
		if(type.equals("0")){
			startDate += "-01-01";
			endDate += "-12-31";
		}else if(type.equals("1")){
			startDate += "-01-01";
			endDate += "-06-30";
		}else if(type.equals("2")){
			startDate += "-07-01";
			endDate += "-12-31";
		}else if(type.equals("3")){
			startDate += "-01-01";
			endDate += "-03-31";
		}else if(type.equals("4")){
			startDate += "-04-01";
			endDate += "-06-30";
		}else if(type.equals("5")){
			startDate += "-07-01";
			endDate += "-09-30";
		}else if(type.equals("6")){
			startDate += "-10-01";
			endDate += "-12-31";
		}
		List list = dao.findXgFromCode(areaCode, startDate, endDate);
		request.setAttribute("list", list);
		request.setAttribute("organ", SystemSession.getUserOrgan(request));
		return mapping.findForward("detail");
	}
	
	public ActionForward xgXls(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		ViewCaseDAO dao = new ViewCaseDAO(request, response);
		String areaCode = request.getParameter("areaCode");
		String year = request.getParameter("year");
		String type = request.getParameter("type");
		String startDate = year;
		String endDate = year;
		if(type.equals("0")){
			startDate += "-01-01";
			endDate += "-12-31";
		}else if(type.equals("1")){
			startDate += "-01-01";
			endDate += "-06-30";
		}else if(type.equals("2")){
			startDate += "-07-01";
			endDate += "-12-31";
		}else if(type.equals("3")){
			startDate += "-01-01";
			endDate += "-03-31";
		}else if(type.equals("4")){
			startDate += "-04-01";
			endDate += "-06-30";
		}else if(type.equals("5")){
			startDate += "-07-01";
			endDate += "-09-30";
		}else if(type.equals("6")){
			startDate += "-10-01";
			endDate += "-12-31";
		}
		List list = dao.findXgFromCode(areaCode, startDate, endDate);
		request.setAttribute("list", list);
		request.setAttribute("organ", SystemSession.getUserOrgan(request));
		return mapping.findForward("xls");
	}
	
	
}